/**   
 * @Title: NativeCompiler.java 
 * @Package: com.rename.icode.model 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-4-17 上午10:09:20 
 * @version  
 */

package com.rename.icode.model;

import com.rename.icode.comm.Constants;
import com.rename.icode.utils.CommandUtil;
import com.rename.icode.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 本地编译
 * @author Administrator
 * @date 2015-4-17 上午10:09:20
 * @version V1.0
 */

public class NativeCompiler implements ICompiler {

	private static final String TAG = "NativeCompiler";
	private String result = null;

	public NativeCompiler() {

	}

	static class Loader {
		static final NativeCompiler INSTANCE = new NativeCompiler();
	}

	public static NativeCompiler getInstance() {
		return Loader.INSTANCE;
	}

	/**
	 * Description
	 * 
	 * @param filePath
	 * @return
	 * @see com.rename.icode.model.ICompiler#compile(java.lang.String)
	 */

	@Override
	public String compile(CodeFile codeFile) {
		String filePath = codeFile.getPath();
		if(filePath == null){
			return null;
		}
		
		if(!filePath.endsWith(".c")){
//			List<String> temp = new ArrayList<String>();
//			temp.add("本地编译只支持C代码，其他代码只能登录后再编译");
//			return temp;
			return "本地编译只支持C代码，其他代码只能登录后再编译";
		}
		
		String compileCmd = "gcc " + filePath + " -o " + Constants.EXE_FILE;
		String[] commands = new String[] { Constants.CMD_EXPORT_GCCHOME,
				Constants.CMD_EXPORT_GCCPATH, Constants.CMD_EXPORT_PATH,
				Constants.CMD_GCC_ROOT, compileCmd, Constants.CMD_RUN };
		List<String> results = CommandUtil.excuteCommand(commands); // 编译运行
		LogUtil.i(TAG, "results is null is " + (null == results));
		
		if(results != null){
			result = "";
			for(String temp : results){
				result += temp + "\n";
			}
		}
		
		return result;
	}

}
